package com.ossjk.config.mail;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.qlh.system.entity.Dictionary;
import com.ossjk.qlh.system.service.IDictionaryService;

import cn.hutool.extra.mail.MailAccount;

@Configuration
public class MailConfig {
	@Autowired
	private IDictionaryService iDictionaryService;

	@Bean
	public MailAccount mailAccount() {
		MailAccount account = new MailAccount();
		QueryWrapper<Dictionary> queryWrapper = new QueryWrapper<Dictionary>();
		queryWrapper.eq("type", 3);
		List<Dictionary> dictionaries = iDictionaryService.list(queryWrapper);
		for (Dictionary dictionary : dictionaries) {
			String dkey = dictionary.getDkey();
			String dvalue = dictionary.getDvalue();
			if (CommonUtil.isEquals(dkey, "host")) {
				//设置邮件服务器
				account.setHost(dvalue);
			} else if (CommonUtil.isEquals(dkey, "port")) {
				//设置邮件服务器端口
				account.setPort(CommonUtil.int2(dvalue));
			} else if (CommonUtil.isEquals(dkey, "mailSender")) {
				//设置邮件发送方
				account.setFrom(dvalue);
			} else if (CommonUtil.isEquals(dkey, "user")) {
				//设置邮件发送用户名
				account.setUser(dvalue);
			} else if (CommonUtil.isEquals(dkey, "pass")) {
				//设置邮件发送方授权码
				account.setPass(dvalue);
			}
		}
		account.setAuth(true);
		//使用ssl协议
		account.setSslEnable(true);
		return account;
	}
}
